// import Layout from "@/page/index/index.vue";
import website from "@/config/website";
import { isURL } from "@/utils/validate";

export const findFirstPath = (menu) => {
  const propsConfig = website.menu.props;
  const children = menu[propsConfig.children];
  if (!menu || !children) return "";
  if (children.length === 0) {
    return menu.path;
  } else {
    return findFirstPath(children[0]);
  }
};
export const filterAsyncRouter = (menus, first) => {
  const routerList = [];
  const propsConfig = website.menu.props;
  const propsDefault = {
    label: propsConfig.label || "name",
    path: propsConfig.path || "path",
    icon: propsConfig.icon || "icon",
    children: propsConfig.children || "children",
    meta: propsConfig.meta || "meta",
  };
  if (menus.length === 0) return;
  for (let i = 0; i < menus.length; i++) {
    const oMenu = menus[i];
    let path = oMenu[propsDefault.path];
    let component = "views" + oMenu[propsDefault.path];
    let name = oMenu[propsDefault.label];
    let icon = oMenu[propsDefault.icon];
    let children = oMenu[propsDefault.children];
    let meta = oMenu[propsDefault.meta] || {};
    const isChild = children.length !== 0;
    const oRouter = {
      path: path,
      component(resolve) {
        // 判断是否为首路由
        if (first) {
          require(["@/page/index"], resolve);
          return;
          // 判断是否为多层路由
        } else if (isChild && !first) {
          require(["@/page/layout"], resolve);
          return;
          // 判断是否为最终的页面视图
        } else {
          require([`@/${component}.vue`], resolve);
        }
      },
      name: name,
      icon: icon,
      meta: meta,
      redirect: (() => {
        if (isChild && !isURL(path)) return findFirstPath(oMenu);
        else return "";
      })(),
      children: !isChild
        ? (() => {
            if (first) {
              // 这里的isURL判断，因为这个网站有使用 iframe。所以需要判断是否为网页链接
              if (!isURL(path)) oMenu[propsDefault.path] = `${path}/index`;
              return [
                {
                  component(resolve) {
                    require([`../${component}.vue`], resolve);
                  },
                  icon: icon,
                  name: name,
                  meta: meta,
                  path: "index",
                },
              ];
            }
            return [];
          })()
        : (() => {
            /**
             * 这里是重点，当有子路由的时候 会再去执行 formatRoutes 方法，然后又会有一个新的 aMenu for循环。
             * 最后返回的是一个数组 aRouter 这个数组就会作为 childen的值被 return
             */
            return filterAsyncRouter(children, false);
          })(),
    };
    routerList.push(oRouter);
  }
  return routerList;
  // return menus.filter((route) => {
  //   if (route.component) {
  //     // Layout组件特殊处理
  //     if (route.component === "Layout") {
  //       route.component = Layout;
  //     } else {
  //       route.component = loadView(route.component);
  //     }
  //   }
  //   // 如果有子路由，则采用递归的方式
  //   if (route.children != null && route.children && route.children.length) {
  //     route.children = filterAsyncRouter(route.children);
  //   }
  //   return true;
  // });
};

export const loadView = (view) => {
  // 路由懒加载
  return (resolve) => require([`@/views/${view}`], resolve);
};
